package com.disapand.strongarmy.listener

import com.disapand.strongarmy.service.*
import com.disapand.strongarmy.utils.*
import org.springframework.boot.*
import org.springframework.context.*
import org.springframework.stereotype.*
import org.springframework.util.*
import org.springframework.web.bind.annotation.*

/**
 *
 * @auth disapand
 * @date 2021/10/26 - 16:52 - 星期二
 *
 */
@Component
class PermissionInitListener(
    private val context: ApplicationContext,
    private val permissionService: PermissionService
    ) : ApplicationRunner {

    override fun run(args: ApplicationArguments) {
        val environment = context.environment
        // 配置文件设置的 context-path
        var property = environment.getProperty("server.servlet.context-path")
        if (property.isNullOrBlank()) {
            property = ""
        } else if (!property.startsWith("/")){
            property += "/"
        }

        val beanMap = context.getBeansWithAnnotation(RestController::class.java)
        beanMap.forEach {
            val value = it.value
            // value.javaClass 获取到的有可能是 CGLIB 代理的类
            // 通过 ClassUtils.getUserClass 可以获取到原始类
            val valueClass = ClassUtils.getUserClass(value.javaClass)
            val mapping = valueClass.getAnnotation(RequestMapping::class.java)
            // 类上的 RequestMapping 值
            var mappingValue = ""
            var mappingLabel = ""
            if (mapping != null) {
                val mappingArray = mapping.value
                mappingLabel = mapping.name
                if (mappingArray.size == 1) {
                    mappingValue = SaUtils.generateURI(mappingArray[0])
                }
            }

            val methods = valueClass.methods
            methods.forEach methodHandle@{ method ->
                // println("类：${it.key} ---- 方法：$method")
                val permissionLabel: String
                var permission = property + mappingValue
                val methodType: String
                // 获取方法上的 url
                val requestMappingUrl = method.getAnnotation(RequestMapping::class.java)
                val getMappingUrl = method.getAnnotation(GetMapping::class.java)
                val postMappingUrl = method.getAnnotation(PostMapping::class.java)
                val putMappingUrl = method.getAnnotation(PutMapping::class.java)
                val deleteMappingUrl = method.getAnnotation(DeleteMapping::class.java)

                if (requestMappingUrl != null && requestMappingUrl.value.size == 1) {
                    permission += SaUtils.generateURI(requestMappingUrl.value[0])
                    methodType = requestMappingUrl.method[0].name
                    permissionLabel = requestMappingUrl.name
                } else if (getMappingUrl != null && getMappingUrl.value.size == 1) {
                    permission += SaUtils.generateURI(getMappingUrl.value[0])
                    methodType = "GET"
                    permissionLabel = getMappingUrl.name
                } else if (postMappingUrl != null && postMappingUrl.value.size == 1) {
                    permission += SaUtils.generateURI(postMappingUrl.value[0])
                    methodType = "POST"
                    permissionLabel = postMappingUrl.name
                } else if (putMappingUrl != null && putMappingUrl.value.size == 1) {
                    permission += SaUtils.generateURI(putMappingUrl.value[0])
                    methodType = "PUT"
                    permissionLabel = putMappingUrl.name
                } else if (deleteMappingUrl != null && deleteMappingUrl.value.size == 1) {
                    permission += SaUtils.generateURI(deleteMappingUrl.value[0])
                    methodType = "DELETE"
                    permissionLabel = deleteMappingUrl.name
                } else {
                    return@methodHandle
                }

                println("接口名：$permissionLabel --------- 接口：$permission ------ 请求方法：$methodType")

                permissionService.addOrUpdate(property + mappingValue, mappingLabel, permissionLabel, methodType, permission)
            }
        }
    }

}